<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="initial-scale=1.0, user-scalable=no, width=device-width" name="viewport">
    <title>下属行政区查询</title>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/2.1.1-rc2/jquery.js"
        type="text/javascript"></script>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=d5c3d02965103b4abb1aaed7588b9958&plugin=AMap.DistrictSearch"
        type="text/javascript"></script>
<script type="text/javascript">
    var district, polygons = [];
    var totalData = [];
    var level = ["country", 'province', 'city', 'district', 'street'];
    //行政区划查询
    var opts = {
        adcode: "100000", //440000
        subdistrict: 1,   //返回下一级行政区
        showbiz: false,  //最后一级返回街道信息
        extensions: 'all',
    };
    var temp = undefined;
    district = new AMap.DistrictSearch(opts);//注意：需要使用插件同步下发功能才能这样直接使用

    var counter = 0;

    function getData(opts) {
        counter++;
        district.search(opts.adcode, $.proxy(function (c, status, result) {
            if (status === 'complete') {
                doGgetData(result.districtList[0], c);
            }
            counter--;
        }, this, opts));
    }

    function doGgetData(data, c) {
        var bounds = data.boundaries;
        let geoJson = undefined;
        if (data.level !== 'country' && bounds) {
            var cooo = [];
            for (var i = 0, l = bounds.length; i < l; i++) {
                var out = bounds[i];
                var coo = [];
                for (var j = 0; j < out.length; j++) {
                    coo.push([out[j].lng, out[j].lat]);
                }
                coo = [coo];
                cooo.push(coo);
            }
            var type = 'Polygon';
            if (cooo.length === 1) {
                type = 'Polygon';
                cooo = cooo[0];
            } else {
                type = 'MultiPolygon';
            }
            geoJson = JSON.stringify({
                "type": type,
                "coordinates": cooo
            });
        }
        debugger;
        var obj = {
            areaName: data.name,
            center: JSON.stringify([data.center.lng, data.center.lat]),
            geoJson: geoJson,
            areaType: type,
            code: data.adcode,
            parentCode: c.pcode || "-1",
            level: data.level.toUpperCase()
        };


        if (temp === undefined) {
            temp = confirm("是否保存数据！");
        }
        if (temp) {
            $.ajax({
                type: 'post',
                url: "http://localhost:8080/sqxm/hyRegion/commit",
                dataType: 'json',
                data: JSON.stringify(obj),
                contentType: 'application/json',
                success: function (result) {
                    if (result.success) {
                        // console.log(data.name + " : " + data.adcode)
                        // console.log(JSON.stringify(obj));
                        // alert("保存成功！");
                    }
                }
            });
        }
        // totalData.push(obj);
        // var subList = data.districtList;
        // if (subList && subList.length > 0) {//
        //     if (subList[0].level === "street") {
        //         var arr = [];
        //         for (var i = 0, l = subList.length; i < l; i++) {
        //             var index = i;
        //             var sub = subList[i];
        //             var ss = index + "";
        //             if (ss.length === 1) {
        //                 ss = "0" + ss;
        //             }
        //             var obj = {
        //                 areaName: sub.name,
        //                 center: JSON.stringify([sub.center.lng, sub.center.lat]),
        //                 areaType: type,
        //                 code: sub.adcode + ss,
        //                 parentCode: data.adcode || "-1",
        //                 level: sub.level.toUpperCase()
        //             };
        //             arr.push(obj);
        //         }
        //
        //         counter++;
        //         setTimeout(function () {
        //             counter--;
        //             $.ajax({
        //                 type: 'post',
        //                 url: "http://localhost:8080/sqxm/hyRegion/commitList",
        //                 dataType: 'json',
        //                 data: JSON.stringify(arr),
        //                 contentType: 'application/json',
        //                 success: function (result) {
        //                     if (result.success) {
        //                         // console.log(sub.name + " : " + sub.adcode)
        //                         // console.log(JSON.stringify(obj));
        //                         // alert("保存成功！");
        //                     }
        //                 }
        //             });
        //         }, Math.random() * 180000);
        //     } else {
        //         for (var i = 0, l = subList.length; i < l; i++) {
        //             (function (sub, index) {
        //                 if (sub.level === "country"
        //                     || sub.level === "province"
        //                     || sub.level === "city"
        //                     || sub.level === "district") {
        //
        //                     var newOpts = {
        //                         pcode: data.adcode,
        //                         adcode: sub.adcode,
        //                         subdistrict: 1,   //返回下一级行政区
        //                         showbiz: false,  //最后一级返回街道信息
        //                         extensions: 'all',
        //                     };
        //
        //                     counter++;
        //                     setTimeout(function () {
        //                         counter--;
        //                         this.getData(newOpts);
        //                     }, Math.random() * 180000);
        //                 } else if (sub.level === "street") {
        //
        //                 }
        //             })(subList[i], i);
        //         }
        //     }
        //
        // }
    }

    getData(opts);

    // var oooo = setInterval(function () {
    //     if (counter == 0) {
    //         clearInterval(oooo);
    //         var nnn = {
    //
    //         }
    //         for(var i = 0 ; i < totalData.length; i++){
    //             nnn[totalData[i].adcode] = totalData[i];
    //         }
    //         console.log(JSON.stringify(totalData));
    //         debugger;
    //         if (confirm("是否保存数据！")) {
    //             // $.ajax({
    //             //     type: 'post',
    //             //     url: "http://localhost:8080/demo/area/commitAreas",
    //             //     dataType: 'json',
    //             //     data: JSON.stringify(totalData),
    //             //     contentType: 'application/json',
    //             //     success: function (result) {
    //             //         if (result.success) {
    //             //             alert("保存成功！");
    //             //         }
    //             //     }
    //             // });
    //         }
    //     }
    // }, 1000);

</script>

</body>
</html>
